package com.dgut.uhrs.jwt;

import io.jsonwebtoken.*;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.UUID;

public class test {

    private long time = 1000*60*60*24;
    private String signature = "admin";

    @Test
    public void jwt(){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
                //header
                .setHeaderParam("typ","jwt")
                .setHeaderParam("alg","HS256")
                //payload
                .claim("username","sakura")
                .claim("role","admin")
                .setSubject("admin-test")
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();
        System.out.println(jwtToken);
    }

    @Test
    public void parse(){
        String parseToken = "eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InNha3VyYSIsInJvbGUiOiJhZG1pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2MjM0NjU0NjIsImp0aSI6IjExZTE1ZGUyLWIwNDYtNGIwMS04NTIxLTZjYzc4MDE1NTQ2ZiJ9.qiMCUMP7po7cK-X4sA4_tNZhAtBpKzhvUVpXRUSJv7E";
        JwtParser jwtParser = Jwts.parser();
        Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(parseToken);
        Claims claimsJwsBody = claimsJws.getBody();
        System.out.println(claimsJwsBody);

    }
}
